# -*- coding: UTF-8 -*-
"""
@Filename:exerc3.py
@author:Cora
@time:2025-04-11
"""
import jieba

excludes = {"将军", "却说", "荆州", "二人", "不可", "不能", "如此", "商议", "如何"}
txt = open("三国演义.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)

# 创建一个空字典，用于存储人名及其出现的次数
counts = {}

for word in words:
    if len(word) == 1:
        continue
    # 对同一个人物的不同称谓进行统一处理
    elif word == "诸葛亮" or word == "孔明曰":
        rword = "孔明"
    elif word == "关公" or word == "云长":
        rword = "关羽"
    elif word == "玄德" or word == "玄德曰":
        rword = "刘备"
    elif word == "孟德" or word == "丞相":
        rword = "曹操"
    else:
        rword = word
    # 统计词汇的出现次数，更新字典
    counts[rword] = counts.get(rword, 0) + 1

# 从统计结果中排除不需要的词汇
for word in excludes:
    if word in counts:
        del counts[word]

# 将字典转换为列表，并按出现次数从高到低排序
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)

# 输出统计结果
for i in range(len(items)):
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))